/**
 * 联系人模型
 */
define(function(require, exports, module) {
 	var Backbone = require('backbone'),
        _ = require('underscore'),
        Pinyin = require('pinyin');
    module.exports = Backbone.Model.extend({
    	defaults: {
    		account: '', //帐号
    		nick: '', //昵称
    		remark: '', //备注名，用于显示
    		head: '', //头像图片地址
    		sex: 'N', //性别：M-男，F-女，N-未知
    		pinyin: ''
    	},
    	idAttribute: 'objectId',
    	initialize: function () {
    		
    		this.on('change:remark', this.onChangeRemark);

    		if(this.get('nick').length===0) this.set('nick', this.get('account'));

    		if(this.get('remark').length===0) this.set('remark', this.get('nick'));


    	},
    	validate: function(attrs, options) {
    		if(attrs.account.length===0) return '帐号不能为空';
    		if(_.indexOf(['M','F','N'], attrs.sex) < 0) return '性别'+attrs.sex+'无效';
    	},
        loadFromRemote: function(user) {
            user = user || {};
            var data = {
                objectId: user.objectId,
                account: user.username,
                nick: user.nick || user.username,
                remark: user.nick || user.username,
                head: user.head,
                sex: user.sex
            };
            this.set( data );
        },
        setRemark: function(val) {
            if(val && _.isString(val) && val.length > 0) {
                this.set('remark', val);
            }
        },
    	onChangeRemark: function(model, value) {
    		var pinyin = Pinyin(value, {
    			style: Pinyin.STYLE_FIRST_LETTER
    		});
    		this.attributes.pinyin = _.flatten(pinyin).join('').toUpperCase();
    	}
    });
 });